package com.caipiao.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterOutputStream;
// 添加ZIP文件处理所需的导入
import java.util.zip.ZipInputStream;
import java.util.zip.ZipEntry;
import java.io.ByteArrayInputStream;
import java.util.List;
import java.util.ArrayList;

/*
import java.io.DataOutputStream;
import com.jcraft.jzlib.JZlib;
import com.jcraft.jzlib.ZInputStream;
import com.jcraft.jzlib.ZOutputStream;
*/

@SuppressWarnings("deprecation")
public class Zip
{
	/**
	 * 压缩数据
	 *
	 * @param object
	 * @return
	 * @throws IOException
	 */
	public static byte[] jzlib(byte[] object) throws IOException
	{
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		DeflaterOutputStream zos = new DeflaterOutputStream(bos);
		zos.write(object);
		zos.close();

		return bos.toByteArray();
	}

	/**
	 * 解压被压缩的数据
	 *
	 * @param object
	 * @return
	 * @throws IOException
	 */
	public static byte[] unjzlib(byte[] object) throws IOException
	{
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		InflaterOutputStream zos = new InflaterOutputStream(bos);
		zos.write(object);
		zos.close();

		return bos.toByteArray();
	}
	
	/**
	 * 读取压缩包文件目录列表
	 * 
	 * @param zipData 压缩包数据
	 * @return 文件路径列表
	 * @throws IOException
	 */
	public static List<String> listZipEntries(byte[] zipData) throws IOException {
		List<String> entries = new ArrayList<String>();
		ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(zipData));
		ZipEntry entry;
		
		while ((entry = zis.getNextEntry()) != null) {
			entries.add(entry.getName());
			zis.closeEntry();
		}
		
		zis.close();
		return entries;
	}

	/**
	 * 压缩数据
	 * 
	 * @param object
	 * @return
	 * @throws IOException
	 */

	/*
	public static byte[] jzlib(byte[] object)
	{

		byte[] data = null;
		ByteArrayOutputStream out = null;
		ZOutputStream zOut = null;
		DataOutputStream objOut = null;

		try
		{
			out = new ByteArrayOutputStream();
			zOut = new ZOutputStream(out, JZlib.Z_DEFAULT_COMPRESSION);
			objOut = new DataOutputStream(zOut);
			objOut.write(object);
			objOut.flush();
			zOut.close();
			data = out.toByteArray();

		} catch (IOException e)
		{
			e.printStackTrace();
			data = null;
		} finally
		{
			if (out != null)
			{
				try
				{
					out.close();
				} catch (IOException e)
				{
				}
			}
		}
		return data;
	}
	*/

	/**
	 * 解压被压缩的数据
	 * 
	 * @param object
	 * @return
	 * @throws IOException
	 */

	/*
	public static byte[] unjzlib(byte[] object)
	{

		byte[] data = null;
		ByteArrayInputStream in = null;
		ZInputStream zIn = null;
		ByteArrayOutputStream baos = null;
		try
		{
			in = new ByteArrayInputStream(object);
			zIn = new ZInputStream(in);
			byte[] buf = new byte[1024];
			int num = -1;
			baos = new ByteArrayOutputStream();
			while ((num = zIn.read(buf, 0, buf.length)) != -1)
			{
				baos.write(buf, 0, num);
			}
			data = baos.toByteArray();
			baos.flush();
			// baos.close();
			// zIn.close();
			// in.close();

		} catch (IOException e)
		{
			e.printStackTrace();
			data = null;
		} finally
		{
			if (zIn != null)
			{
				try
				{
					zIn.close();
				} catch (IOException e)
				{
				}
			}
			if (in != null)
			{
				try
				{
					in.close();
				} catch (IOException e)
				{
				}
			}
			if (baos != null)
			{
				try
				{
					baos.close();
				} catch (IOException e)
				{
				}
			}
		}
		return data;
	}
	*/
}